﻿<%@ Page Title="" Language="C#" MasterPageFile="~/TsSite.Master" AutoEventWireup="true" CodeBehind="CreateSubscriptionInvoice.aspx.cs" Inherits="TireService.Web.View.AdminView.CreateSubscriptionInvoice" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1.Export, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxGridView.Export" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxLoadingPanel" TagPrefix="dx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <table cellpadding="0" cellspacing="2" border="0">
        <tr>
            <td>
                <img id="imgInvoice" alt="" src="~/Images/Medium/money.png" runat="server" style="width: 32px; height: 32px" />
            </td>
            <td valign="middle">
                <h3><asp:Label ID="lblCreateSubscriptionInvoice" runat="server" Text="Forny abonnementer og generer faktura'er"></asp:Label></h3>
            </td>
        </tr>
    </table>
    <hr />

    <dx:ASPxLoadingPanel ID="LoadingPanel" Text="Vent venligst..." ClientInstanceName="LoadingPanel" runat="server" Modal="True">
    </dx:ASPxLoadingPanel>

    <!-- Javascript of action dropdown list -->
    <script language="javascript" type="text/javascript">

        // Function that handles method selected in dropdown list
        function MethodClicked() {

            // Validate values
            txtStartdate.Validate();
            txtEnddate.Validate();

            var isFormValid = ASPxClientEdit.ValidateGroup("SelectMethod");
            if (isFormValid) {

                // Get selected method from dropdownlist
                var id = ddlMethod.GetValue();

                if (id != null && id != 0) {

                    var StartDate = new Date(txtStartdate.GetValue());
                    var stringStartDate = StartDate.getDate() + "-" + (1 + StartDate.getMonth()) + "-" + StartDate.getFullYear();
                    var EndDate = new Date(txtEnddate.GetValue());
                    var stringEndDate = EndDate.getDate() + "-" + (1 + EndDate.getMonth()) + "-" + EndDate.getFullYear();

                    var input = id + '|' + stringStartDate + '|' + stringEndDate;

                    // Renew customers running on PBS
                    if (id == 1) {
                        gvSubscriptionInvoice.PerformCallback(input);
                    }

                    // Renew customers where invoice is send via e-mail
                    if (id == 2) {
                        gvSubscriptionInvoice.PerformCallback(input);
                    }

                    // Renew customers that is manually invoiced
                    if (id == 3) {
                        gvSubscriptionInvoice.PerformCallback(input);
                    }
                }
                else {
                    alert('Ingen metode valgt');
                }
            }
        }

        // Function that runs when server side completed processing
        function OnCallbackGenerateInvoicesCompleted(s, e) {
            LoadingPanel.Hide();
            var id = ddlMethod.GetValue();
            var StartDate = new Date(txtStartdate.GetValue());
            var stringStartDate = StartDate.getDate() + "-" + (1 + StartDate.getMonth()) + "-" + StartDate.getFullYear();
            var EndDate = new Date(txtEnddate.GetValue());
            var stringEndDate = EndDate.getDate() + "-" + (1 + EndDate.getMonth()) + "-" + EndDate.getFullYear();
            var input = id + '|' + stringStartDate + '|' + stringEndDate;

            gvSubscriptionInvoice.PerformCallback(input);

            alert('Fornyelse og fakturering gennemført');
        }

        // Function that return error in case callback to server failed
        function CallBackError(s, e) {
            LoadingPanel.Hide();
            alert('Fejl opstod: ' + e.message);
        }

        function GenerateInvoices() {
            var answer = confirm("Forny og opret faktura'er for abonnementer i listen?")
            if (answer) {
                // Show loading panel
                LoadingPanel.Show();

                var id = ddlMethod.GetValue();
                var StartDate = new Date(txtStartdate.GetValue());
                var stringStartDate = StartDate.getDate() + "-" + (1 + StartDate.getMonth()) + "-" + StartDate.getFullYear();
                var EndDate = new Date(txtEnddate.GetValue());
                var stringEndDate = EndDate.getDate() + "-" + (1 + EndDate.getMonth()) + "-" + EndDate.getFullYear();
                var input = id + '|' + stringStartDate + '|' + stringEndDate;

                callGenerateInvoices.PerformCallback(input);
            } 
        }

    </script>

    <dx:ASPxCallback ID="callGenerateInvoices" ClientInstanceName="callGenerateInvoices" runat="server" OnCallback="callGenerateInvoices_Callback">
        <ClientSideEvents CallbackError="CallBackError" CallbackComplete="OnCallbackGenerateInvoicesCompleted" />
    </dx:ASPxCallback>

    <table border="0" cellspacing="0" cellpadding="5" width="100%">
        <tr>
            <td style="width:50px">
                <dx:ASPxLabel ID="lblExpirePeriod" runat="server" Text="Periode">
                </dx:ASPxLabel>
            </td>
            <td style="width:150px">
                <dx:ASPxDateEdit ID="txtStartdate" ClientInstanceName="txtStartdate" runat="server">
                    <ValidationSettings CausesValidation="true" ValidationGroup="SelectMethod" RequiredField-IsRequired="true" RequiredField-ErrorText="Vælg startdato for periode" SetFocusOnError="true" ErrorDisplayMode="ImageWithTooltip">
                        <RequiredField IsRequired="True" ErrorText="V&#230;lg startdato for periode"></RequiredField>
                    </ValidationSettings>
                </dx:ASPxDateEdit>
            </td>
            <td style="width:150px">
                <dx:ASPxDateEdit ID="txtEnddate" ClientInstanceName="txtEnddate" runat="server">
                    <ValidationSettings CausesValidation="true" ValidationGroup="SelectMethod" RequiredField-IsRequired="true" RequiredField-ErrorText="Vælg slutdato for periode" SetFocusOnError="true" ErrorDisplayMode="ImageWithTooltip">
                        <RequiredField IsRequired="True" ErrorText="V&#230;lg slutdato for periode"></RequiredField>
                    </ValidationSettings>
                </dx:ASPxDateEdit>
            </td>
            <td style="width:150px">
                <dx:ASPxComboBox ID="ddlMethod" ClientInstanceName="ddlMethod" runat="server" Width="150px">
                    <Items>
                        <dx:ListEditItem Selected="true" Value="0" Text="-- Faktura metode --" />
                        <dx:ListEditItem Value="1" Text="Kunder tilmeldt PBS" />
                        <dx:ListEditItem Value="2" Text="Kunder med e-mail fakturering" />
                        <dx:ListEditItem Value="3" Text="Kunder med manuel fakturering" />
                    </Items>
                </dx:ASPxComboBox>
            </td>
            <td style="width:75px">
                <dx:ASPxButton ID="btnSubmitMethod" runat="server" Text="Vis liste" AutoPostBack="False" UseSubmitBehavior="False" Width="75px">
                    <ClientSideEvents Click="function(s, e) { MethodClicked(); }" />
                </dx:ASPxButton>
            </td>
            <td style="width:150px">
                <dx:ASPxComboBox ID="ddlInvoiceCount" ClientInstanceName="ddlInvoiceCount" runat="server" Width="150px">
                    <Items>
                        <dx:ListEditItem Value="10" Text="Opret 10" />
                        <dx:ListEditItem Value="20" Selected="true" Text="Opret 20" />
                        <dx:ListEditItem Value="40" Text="Opret 40" />
                    </Items>
                </dx:ASPxComboBox>
            </td>
            <td style="width:75px">
                <dx:ASPxButton ID="btnGenerateInvoices" runat="server" Text="Opret faktura'er" AutoPostBack="False" UseSubmitBehavior="False" Width="75px">
                    <ClientSideEvents Click="function(s, e) { GenerateInvoices(); }" />
                </dx:ASPxButton>
            </td>
            <td>
            </td>
            <%--<td style="width:75px">
                <dx:ASPxButton ID="btnExport" runat="server" Text="Exporter" OnClick="btnExport_Click" Width="75px" >
                </dx:ASPxButton>
            </td>--%>
        </tr>
    </table>          
    <hr />

    <dx:ASPxGridView ID="gvSubscriptionInvoice" runat="server" ClientInstanceName="gvSubscriptionInvoice" 
        KeyFieldName="TaskId" Width="100%" AutoGenerateColumns="False" EnableCallBacks="True" OnCustomCallback="gvSubscriptionInvoice_CustomCallback">
        <SettingsLoadingPanel Text="Henter&amp;hellip;" />
        <Border BorderWidth="0px" />
        <Settings ShowHeaderFilterButton="true" ShowGroupPanel="true" ShowFilterRow="True" />
        <SettingsPager AlwaysShowPager="False" PageSize="20" Position="TopAndBottom">
            <Summary 
                AllPagesText="Sider: {0} - {1} ({2} abonnementer)" 
                Text="Side {0} af {1} ({2} abonnementer)" />
        </SettingsPager>
        <SettingsBehavior AllowFocusedRow="false" ConfirmDelete="True" AllowGroup="true" AllowDragDrop="true" />
        <Columns>
            <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="1">
                    <HeaderTemplate>
                        <input type="checkbox" onclick="gvSubscriptionInvoice.SelectAllRowsOnPage(this.checked);" style="vertical-align:middle;" title="Vælg/Fravælg alle linjer på siden"></input>
                    </HeaderTemplate>
                    <HeaderStyle Paddings-PaddingTop="1" Paddings-PaddingBottom="1" 
                        HorizontalAlign="Center">
                    <Paddings PaddingTop="1px" PaddingBottom="1px"></Paddings>
                    </HeaderStyle>
            </dx:GridViewCommandColumn>
            <dx:GridViewDataColumn FieldName="CustomerSubscriptionId" VisibleIndex="2" Caption="Abonnement nr">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="SubscriptionDescription" VisibleIndex="2" Caption="Abonnement">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="LastRenewalDate" VisibleIndex="2" Caption="Sidst fornyet">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="NextRenewalDate" VisibleIndex="2" Caption="Næste fornyelse">
            </dx:GridViewDataColumn>
            <dx:GridViewDataHyperLinkColumn FieldName="CustomerId" VisibleIndex="3" Caption="Kundenr">
                <PropertiesHyperLinkEdit NavigateUrlFormatString="~/View/CustomerView/EditCustomerData.aspx?CustomerId={0}" TextField="CustomerId" Target="_blank">
                </PropertiesHyperLinkEdit>
            </dx:GridViewDataHyperLinkColumn>
            <dx:GridViewDataColumn FieldName="CompanyName" VisibleIndex="4" Caption="Firma">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="Name" VisibleIndex="5" Caption="Navn">
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="RegistrationNumber" VisibleIndex="6" Caption="Regnr">
            </dx:GridViewDataColumn>
        </Columns>
    </dx:ASPxGridView>              
                
    <dx:ASPxGridViewExporter ID="gvExport" runat="server" GridViewID="gvSubscriptionInvoice">
    </dx:ASPxGridViewExporter>

</asp:Content>
